#include <iostream>
#include"../../client/rpc_caller.hpp"
#include"../../common/detail.hpp"
#include "../../client/rpc_client.hpp"

// void RpconMessage(const rpc::BaseConnection::ptr &conn, const rpc::RpcResponse::ptr &msg)
// {
//     std::cout << "收到Rpc响应消息:" << (int)msg->getRecode() << std::endl;
//     std::string str = msg->serilize();
//     std::cout << str << std::endl;
// }

// void TopiconMessage(const rpc::BaseConnection::ptr &conn, const rpc::TopicResponse::ptr &msg)
// {
//     std::cout << "收到Topic响应消息:" << (int)msg->getMytype() << std::endl;
//     std::string str = msg->serilize();
//     std::cout << str << std::endl;
// }

/////////第一阶段测试代码

// auto requestor = std::make_shared<rpc::client::Requestor>();
// auto caller = std::make_shared<rpc::client::RpcCaller>(requestor); // caller需要使用requestor初始化
// auto disp = std::make_shared<rpc::dipatcher>();
// auto cb_rsp = std::bind(&rpc::client::Requestor::onResponse, requestor.get(), std::placeholders::_1, std::placeholders::_2);
// auto cb = std::bind(&rpc::dipatcher::onMessage, disp.get(), std::placeholders::_1, std::placeholders::_2);

// disp->rigisteHandler<rpc::RpcResponse>(rpc::MY_TYPE::RSP_RPC, cb_rsp);
// auto client = rpc::ClientFactory::create("127.0.0.1", 9090);
// // disp->rigisteHandler<rpc::RpcResponse>(rpc::MY_TYPE::RSP_RPC, RpconMessage);
// // disp->rigisteHandler<rpc::TopicResponse>(rpc::MY_TYPE::RSP_TOPIC, TopiconMessage);

// client->setMessageCallback(cb);
// client->connect();
// // 创建请求
// auto conn = client->connection();

void callback(const Json::Value&result)
{
    delog("callback结果：%d", result.asInt());
    std::cout << result.asInt() << std::endl;
}

int main()
{
    rpc::client::RpcClient client(false,"127.0.0.1", 9090);
    Json::Value val;
    Json::Value result;
    val["num1"] = 11;
    val["num2"] = 12;
    delog("client开始调用方法");
    bool ret = client.call("Add", val, result);
    delog("client结束调用方法");
    if (ret)
    {
        std::cout << result.asInt() << std::endl;
    }

    Json::Value pramas;
    rpc::client::RpcCaller::JsonAsync res_future;
    pramas["num1"] = 50;
    pramas["num2"] = 60;
    delog("client开始调用方法");
    ret = client.call("Add", pramas, res_future); // 并不会阻塞
    delog("client结束调用方法");
    if (ret)
    {
        delog("异步调用结果");
       result = res_future.get(); // 用户调用后才会获取结果
        std::cout << result.asInt() << std::endl;
    }

    pramas["num1"] = 90;
    pramas["num2"] = 60;
    delog("client开始回调方法");
    ret = client.call( "Add", pramas, callback); // 并不会阻塞
    delog("client结束回调方法");
    sleep(10);
    return 0;
}